<?php
namespace app\admin\controller;

class Message extends Base{
	
	public function __construct()
	{
		parent::__construct();
	}
	/***
	 *  由于留言内容较少就不写model了
	 */
	public function index($message_type=0)
	{
	    //类别筛选
		if(IS_POST){
	    $message_type = $_POST['message_type'];
		}
		$message_num = M('message')->select();
		$count= count($message_num);
		$Page = $this->getPage($count,C('PAGE_NUMS'));
		if($message_type == 0){
			$message=M('message')->limit($Page->firstRow.','.$Page->listRows)->select();
		}else{
			$message=M('message')->where('message_type='.$message_type)->limit($Page->firstRow.','.$Page->listRows)->select();
		}
		$this->view->assign('message',$message);
		return $this->view->fetch();
	}
	public function addmessage()
	{
		if(IS_POST){
			$data['title']=$_POST['title'];
			$data['content']=$_POST['content'];
			$data['message_type']=$_POST['message_type'];
			$addmsg = M('message')->add($data);
			 if($addmsg){
				 manage_log_success();
		         return $this->success('新增成功', U('Message/index'));
			 }else{
				  manage_log_error();
			 return $this->error('新增失败',U('Message/index'));
			 }
		}
		return $this->view->fetch();
	}
	
	public function scan($id)
	{   
	    $where['id'] = $id;
		$scan = M('message')->where($where)->find();
		$this->view->assign('scan',$scan);
		return $this->view->fetch();
		
	}
	
//判断开始
	//登记
	public function postScan()
	{
		if(IS_POST){
			$where['id'] = $_POST['id'];
			$data['title'] = $_POST['title'];
			$data['name'] = $_POST['name'];
			$data['phone'] = $_POST['phone'];
			$data['email'] = $_POST['email'];
			$data['status'] = $_POST['status'];
			$scan = M('message')->where($where)->save($data);
			 if($scan){
				  manage_log_success();
		         return $this->success('修改成功', U('Message/index'));
			 }else{
				  manage_log_error();
			 return $this->error('修改失败',U('Message/index'));
			 }
		}
	}
		//给我们留言
	public function give_postScan()
	{
		if(IS_POST){
            $where['id'] = $_POST['id'];
			$data['title'] = $_POST['title'];
			$data['name'] = $_POST['name'];
			$data['phone'] = $_POST['phone'];
			$data['email'] = $_POST['email'];
			$data['replay_msg'] = $_POST['replay_msg'];
			$data['content'] = $_POST['content'];
			$data['status'] = $_POST['status'];
			$scan = M('message')->where($where)->save($data);
			 if($scan){
				  manage_log_success();
		         return $this->success('回复成功', U('Message/index'));
			 }else{
				  manage_log_error();
			 return $this->error('回复失败',U('Message/index'));
			 }
		}
	}
		//成果需求
	public function xuqiu_postScan()
	{
		if(IS_POST){
			
            $where['id'] = $_POST['id'];
			$data['title'] = $_POST['title'];
			$data['name'] = $_POST['name'];
			$data['phone'] = $_POST['phone'];
			$data['email'] = $_POST['email'];
			
			$data['xqlb'] = $_POST['xqlb'];
			$data['xqname'] = $_POST['xqname'];
			$data['dwjj'] = $_POST['dwjj'];
			$data['xqjj'] = $_POST['xqjj'];
			$data['jctj'] = $_POST['jctj'];
			$data['hzfs'] = $_POST['hzfs'];
			
			$scan = M('message')->where($where)->save($data);
			 if($scan){
				  manage_log_success();
		         return $this->success('编辑成功', U('Message/index'));
			 }else{
				  manage_log_error();
			 return $this->error('编辑失败',U('Message/index'));
			 }
		}
	}
	//判断 END
	
	public function del($id)
	{
		
	  $reconfirm = $this->reconfirm('删除后无法恢复，是否确认删除？');
        if(!$reconfirm){
			 return $this->error('取消删除', U('Message/index'));
		}
		$del = M('message')->delete($id);
		 if($del){
			 manage_log_success();
		     return $this->success('删除成功', U('Message/index'));
		   }else{
			   manage_log_error();
			 return $this->error('删除失败',U('Message/index'));
		   }
	}
	
	public function status($id)
	{
		$where['id'] = $_POST['id'];
		$data['status']= $_POST['status'];
		$status = M('message')->where($where)->save($data);
		 if($status){
			 manage_log_success();
		 }else{
			  manage_log_error();
		 }
	}
	
	public function mType()
	{
		$mtype = M('mtype')->select();
		$this->view->assign('mtype',$mtype);
		return $this->view->fetch();
	}
	
	public function addType()
	{
		if(IS_POST){
			$data['name'] = $_POST['name'];
			$data['biaoshi'] = $_POST['biaoshi'];
			$addtype = M('mtype')->add($data);
			 if($addtype){
				  manage_log_success();
				 return $this->success('新增成功', U('Message/mType'));
			 }else{
				  manage_log_error();
				 return $this->error('新增失败', U('Message/mType'));
			 }
		}
		return $this->view->fetch();
	}
	
	public function delType($id)
	{
		
		$del_type = M('mtype')->delete($id);
		 if($del_type){
			  manage_log_success();
				 return $this->success('删除成功', U('Message/mType'));
		 }else{
			  manage_log_error();
				 return $this->error('删除失败', U('Message/mType'));
		 }
	}
	
	public function edit($id)
	{
		$where['id'] = $id;
		$edit = M('mtype')->where($where)->find();
		$this->view->assign('edit',$edit);
		return $this->view->fetch();
		
	}
	
	public function post()
	{
		$where['id']=$_POST['id'];
		$data['name']=$_POST['name'];
		$data['biaoshi']=$_POST['biaoshi'];
		$post_edit = M('mtype')->where($where)->save($data);
		 if($post_edit){
			  manage_log_success();
				 return $this->success('编辑成功', U('Message/mType'));
		 }else{
			  manage_log_error();
				 return $this->error('编辑失败', U('Message/mType'));
		 }
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}